package com.bawei.shopping2.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bawei.shopping2.databinding.ItemMainCatogoryBinding
import com.bawei.shopping2.entity.CategoryEntity
import com.bawei.shopping2.entity.GoodsEntity
//一级分类适配器
class MainCategoryAdapter(var list: MutableList<CategoryEntity>):RecyclerView.Adapter<MainCategoryAdapter.MainCategoryViewHolder>() {
    class MainCategoryViewHolder(var itemMainCatogoryBinding: ItemMainCatogoryBinding) : RecyclerView.ViewHolder(itemMainCatogoryBinding.root)
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainCategoryViewHolder {
        var  itemMainCatogoryBinding = ItemMainCatogoryBinding.inflate( LayoutInflater.from(parent.context),parent,false)
        return MainCategoryViewHolder(itemMainCatogoryBinding)
    }

    override fun onBindViewHolder(holder: MainCategoryViewHolder, position: Int) {
        holder.itemMainCatogoryBinding.category = list[position]
        //item的点击事件，lambda
        holder.itemView.setOnClickListener {
            onItemClick(it,position)
        }
        holder.itemView.setOnLongClickListener{
            onItemLongClick(it,position)
            true
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }
    //lambda表达式替换java中接口回调
    lateinit var onItemClick:(v: View, pos: Int) -> Unit   //函数类型 参数是view：position 无返回值
    //lambda表达式替换java中接口回调
    lateinit var onItemLongClick: (v: View, pos: Int) -> Unit //函数类型 参数是view：position 无返回值


    //根据下标返回对应的数据
    fun getDataByPosition(position:Int):CategoryEntity{
        return list[position]
    }

}